package com.matt.docpro.controller;

import com.matt.docpro.config.Utils;
import com.matt.docpro.model.*;
import com.matt.docpro.service.*;
import jakarta.servlet.http.HttpServletRequest;
import org.hibernate.annotations.Check;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * DoctorController
 * Created by on 2024/02/07.
 */
@Controller
public class CheckinController {

    @Autowired
    AppointmentService appointmentService;
    @Autowired
    ReviewService reviewService;
    @Autowired
    DoctorService doctorService;
    @Autowired
    CheckinService checkinService;
    @Autowired
    PatientService patientService;
    @Autowired
    LoginInfoService loginInfoService;

    @RequestMapping("/checkin-dashboard")
    public String init(Model model, HttpServletRequest request) throws Exception {
        String token = Utils.getToken(request);
        User user = loginInfoService.getUserByToken(token);
        if (!user.getRole().equals("3")) {
            return "redirect:/error403";
        }
        List<Appointment> appointments = appointmentService.getAll();
        model.addAttribute("appointments", appointments);
        Checkin checkin = checkinService.getById(user.getId());
        model.addAttribute("checkin", checkin);
        int patientCount = patientService.getCount();
        int doctorCount = doctorService.getCount();
        int appointmentCount = appointmentService.getCount();
        model.addAttribute("patientCount", patientCount);
        model.addAttribute("doctorCount", doctorCount);
        model.addAttribute("appointmentCount", appointmentCount);
        model.addAttribute("user", user);
        return "checkin-dashboard";
    }

    @RequestMapping("/checkin-profile")
    public String myProfile(Model model, HttpServletRequest request) throws Exception {
        String token = Utils.getToken(request);
        User user = loginInfoService.getUserByToken(token);
        Checkin checkin = checkinService.getById(user.getId());
        model.addAttribute("checkin", checkin);
        model.addAttribute("user", user);
        return "checkin-profile";
    }

    @PostMapping("/checkin-profile/save")
    @ResponseBody
    public Map<String, Object> myProfile(Model model, Checkin checkin, MultipartFile iconFile) {
        Map<String, Object> res = new HashMap<>();
        String filePath = Utils.uploadFile(iconFile, checkin.getId());
        if (filePath != null) {
            checkin.setIcon(filePath);
        }
        checkinService.update(checkin);
        res.put("state", "success");
        return res;
    }

    @RequestMapping("/change-password-3")
    public String changePassword(Model model, HttpServletRequest request) {
        String token = Utils.getToken(request);
        User user = loginInfoService.getUserByToken(token);
        Checkin checkin = checkinService.getById(user.getId());
        model.addAttribute("checkin", checkin);
        model.addAttribute("user", user);
        return "change-password-3";
    }

    @RequestMapping("/checkin-appointment")
    public String checkinAppointment(Model model, HttpServletRequest request) {
        String token = Utils.getToken(request);
        User user = loginInfoService.getUserByToken(token);
        Checkin checkin = checkinService.getById(user.getId());
        model.addAttribute("checkin", checkin);
        List<Appointment> appointments = appointmentService.getAll();
        model.addAttribute("appointments", appointments);
        model.addAttribute("user", user);
        return "checkin-appointment";
    }

}
